package com.bsoft.gol.hcs.specialist.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.specialist.bean.request.GroupChronicRecordQo;
import com.bsoft.gol.hcs.specialist.bean.response.TreatmentRecordResponse;
import com.bsoft.gol.hcs.specialist.bean.response.TreatmentTimeInfoResponse;
import com.bsoft.gol.hcs.specialist.entity.FusAnswerTreatmentInfo;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ChenJij on 2021/10/25.
 */
public abstract class FusAnswerTreatmentInfoDAO  extends HibernateSupportDelegateDAO<FusAnswerTreatmentInfo> {
    public FusAnswerTreatmentInfoDAO() {
        this.setKeyField("fusAnswerTreatmentInfoId");
    }


    /**
     * @param reportId
     * @throws
     * @description: 根据表单答案唯一记录id查询
     * @return: specialist.entity.FusAnswerTreatmentInfo
     * @author: tongtp
     * @date: 2020/8/4 17:57
     */
    @DAOMethod(sql = " from FusAnswerTreatmentInfo where reportId = :reportId")
    public abstract FusAnswerTreatmentInfo getByReportId(@DAOParam("reportId") String reportId);


    /**
     *
     * 根据病人id和病种id获取治疗信息
     * @param qo
     * @return
     */
    public List<TreatmentRecordResponse> queryAnswerTreatmentInfoList(GroupChronicRecordQo qo) {
        HibernateStatelessResultAction<List<TreatmentRecordResponse>> action = new AbstractHibernateStatelessResultAction<List<TreatmentRecordResponse>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("SELECT treatment_code as treatmentCode,treatment_name as treatmentName FROM fus_answer_treatment_info WHERE patient_id =:patientId and disease_type_id =:diseaseTypeId ");
                sqlBuilder.append(" and create_time >=:beginTime and create_time <=:endTime order by create_time ");
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setParameter("patientId", qo.getPatientId());
                query.setParameter("diseaseTypeId", qo.getDiseaseTypeId());
                if (CommonUtils.isNotEmpty(qo.getBeginTime())){
                    query.setParameter("beginTime", qo.getBeginTime()+" 00:00:00");
                }
                if (CommonUtils.isNotEmpty(qo.getBeginTime())){
                    query.setParameter("endTime", qo.getEndTime()+" 23:59:59");
                }
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                List<TreatmentRecordResponse> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, TreatmentRecordResponse.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 获取治疗时间信息
     * @param qo
     * @param treatmentName
     * @return
     */
    public List<TreatmentTimeInfoResponse> queryAnswerTreatmentTimeList(GroupChronicRecordQo qo, String treatmentName) {
        HibernateStatelessResultAction<List<TreatmentTimeInfoResponse>> action = new AbstractHibernateStatelessResultAction<List<TreatmentTimeInfoResponse>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.append("SELECT treatment_code as treatmentCode,treatment_name as treatmentName,treatment_time as treatmentTime FROM fus_answer_treatment_info WHERE patient_id =:patientId and disease_type_id =:diseaseTypeId and treatment_name =:treatmentName ");
                sqlBuilder.append(" and create_time >=:beginTime and create_time <=:endTime order by create_time ");
                Query query = ss.createSQLQuery(sqlBuilder.toString());
                query.setParameter("patientId", qo.getPatientId());
                query.setParameter("diseaseTypeId", qo.getDiseaseTypeId());
                query.setParameter("treatmentName", treatmentName);
                if (CommonUtils.isNotEmpty(qo.getBeginTime())){
                    query.setParameter("beginTime", qo.getBeginTime()+" 00:00:00");
                }
                if (CommonUtils.isNotEmpty(qo.getBeginTime())){
                    query.setParameter("endTime", qo.getEndTime()+" 23:59:59");
                }
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                List<TreatmentTimeInfoResponse> voList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        voList.add(ConversionUtils.convert(map, TreatmentTimeInfoResponse.class));
                    }
                } else {
                    voList = new ArrayList<>();
                }
                setResult(voList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    /**
     * 根据用户和病种查询ReportId
     *
     * @param diseaseTypeId
     * @param patientId
     * @param patientMedicalCardType
     * @param patientMedicalCardNumber
     * @param cardType
     * @param cardNumber
     * @return java.util.List<java.lang.String>
     * @throws
     * @author tongtp
     * @date 2021/12/29 13:41
     */
    public List<String> queryReportId(Long diseaseTypeId, String patientId,
                                      Integer patientMedicalCardType, String patientMedicalCardNumber,
                                      String cardType, String cardNumber) {
        HibernateStatelessResultAction<List<String>> action =
                new AbstractHibernateStatelessResultAction<List<String>>() {
                    @Override
                    public void execute(StatelessSession ss) throws Exception {
                        StringBuilder sqlBuilder = new StringBuilder();
                        sqlBuilder.append("select report_id from fus_answer_treatment_info" +
                                "  where disease_type_id = :diseaseTypeId ");
                        sqlBuilder.append(" and ( ");
                        if (CommonUtils.isNotNull(patientId)) {
                            sqlBuilder.append(" patient_id = :patientId ");
                        }
                        if (CommonUtils.isNotNull(patientMedicalCardType) && CommonUtils.isNotNull(patientMedicalCardNumber)) {
                            sqlBuilder.append("  or (patient_medical_card_type = :patientMedicalCardType and" +
                                    " patient_medical_card_number = :patientMedicalCardNumber) ");
                        }
                        if (CommonUtils.isNotNull(cardType) && CommonUtils.isNotNull(cardNumber)) {
                            sqlBuilder.append("  or (card_type = :cardType and" +
                                    " card_number = :cardNumber) ");
                        }
                        sqlBuilder.append(" ) ");
                        Query query = ss.createSQLQuery(sqlBuilder.toString());
                        query.setParameter("diseaseTypeId", diseaseTypeId);
                        if (CommonUtils.isNotNull(patientId)) {
                            query.setParameter("patientId", patientId);
                        }
                        if (CommonUtils.isNotNull(patientMedicalCardType) && CommonUtils.isNotNull(patientMedicalCardNumber)) {
                            query.setParameter("patientMedicalCardType", patientMedicalCardType);
                            query.setParameter("patientMedicalCardNumber", patientMedicalCardNumber);
                        }
                        if (CommonUtils.isNotNull(cardType) && CommonUtils.isNotNull(cardNumber)) {
                            query.setParameter("cardType", cardType);
                            query.setParameter("cardNumber", cardNumber);
                        }
                        List list = query.list();
                        List<String> voList;
                        if (CommonUtils.isNotEmpty(list)) {
                            voList = list;
                        } else {
                            voList = new ArrayList<>();
                        }
                        setResult(voList);
                    }
                };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }
}